package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.ManagerDept;
import com.woniu.market_stall.entity.Role;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/list")
    @ApiOperation(value = "本接口用于分页查看部门")
    public ResultResponse<PageInfo<ManagerDept>> list(
            @ApiParam(value = "当前页的参数")  @RequestParam(value = "now",defaultValue = "1") Integer now,
            @ApiParam(value = "页面大小的参数") @RequestParam(value = "size",defaultValue = "3")Integer size,
            @ApiParam(value = "按条件搜索角色名称") String name) {
        ResultResponse<PageInfo<ManagerDept>> result = null;
        try{
            PageInfo<ManagerDept> pageInfo = departmentService.queryDepartmentByPage(now, size, name);
            result = new ResultResponse<>(200,"查询成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result;

    }
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "本接口用于删除部门")
    public ResultResponse<Void> del(@PathVariable("id")Integer id){
        ResultResponse<Void> result = null;
        try {
            departmentService.removeByKey(id);
            result = new ResultResponse<>(200,"删除部门成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除部门失败!");
        }
        return result ;
    }

    @PostMapping("/add")
    @ApiOperation(value = "本接口用于增加部门")
    public ResultResponse<Void> add(@RequestBody ManagerDept managerDept){
        ResultResponse<Void> result = null;
        try {
            departmentService.add(managerDept);
            result = new ResultResponse<>(200,"添加部门成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加部门失败!");
        }
        return result ;
    }
    @PutMapping("/modify")
    @ApiOperation(value = "本接口用于修改部门")
    public ResultResponse<Void> modify(@RequestBody ManagerDept managerDept){
        ResultResponse<Void> result = null;
        try {
            departmentService.modify(managerDept);
            result = new ResultResponse<>(200,"修改部门成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改部门失败!");
        }
        return result ;
    }

    @GetMapping("/showAllDept")
    @ApiOperation(value = "本接口用于初始化部门信息")
    public ResultResponse<List<ManagerDept>> showAllDept(@ApiParam(value = "当前页的参数") @RequestParam(value = "now",defaultValue = "1") Integer now,
                                                         @ApiParam(value = "页面大小的参数")@RequestParam(value = "size",defaultValue = "3")Integer size,
                                                         @ApiParam(value = "按条件搜索部门") String name){
        ResultResponse<List<ManagerDept>> result = null;
        try {
            List<ManagerDept> list = departmentService.queryAllDept();
            result = new ResultResponse<>(200,"查询部门成功!",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询部门失败!");
        }

        return result ;
    }
}
